//
//  UserInfoViewController.m
//  FootPrint
//
//  Created by 秦 东方 on 4/26/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "UserInfoViewController.h"

@interface UserInfoViewController ()

@end

@implementation UserInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _parser = [[UserInfoParser alloc]init];
        [_parser setDelegate:self];
        
        _imgParser = [[ImageParser alloc]init];
        [_imgParser setDelegate:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_parser reqUserInfo:[GConfig getUserId]];
    
    // add a gesture, when tap in the blank area, hide the keyboard
    UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    tapGr.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapGr];
    
    
    UITapGestureRecognizer * tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnIcon)] autorelease];
    [_imgUser addGestureRecognizer:tapGesture];
    
    
    _scroll.contentSize = CGSizeMake(320, CGRectGetMaxY(_btnSubmit.frame)+5);
    
    UIImage* imgNormal =[[UIImage imageNamed:@"btn_bg_normal.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
    UIImage* imgPress =[[UIImage imageNamed:@"btn_bg_press.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
    
    
    [_btnSubmit setBackgroundImage:imgNormal forState:UIControlStateNormal];
    [_btnSubmit setBackgroundImage:imgPress forState:UIControlStateHighlighted];
    
}



- (void)tapOnIcon
{
    
    PickIconViewController * pickVc = [[[PickIconViewController alloc] initWithNibName:@"PickIconViewController" bundle:nil] autorelease];
    pickVc.pickMode = YES;
    pickVc.disableSysImg = YES;
    pickVc.pickDelegate = self;
    [pickVc setUserMode];
    [self.navigationController pushViewController:pickVc animated:YES];
    
}


- (void) viewTapped:(UITapGestureRecognizer*)tapGr{
    [_textSaid resignFirstResponder];
    [_textPwd1 resignFirstResponder];
    [_textPwd2 resignFirstResponder];
    [_scroll setContentOffset:CGPointZero animated:YES];
}


#pragma mark - PickIconDelegate

- (void)didPickSysImage:(NSString*)sysImgName
{
    
}

- (void)didPickCustomImage:(long long)imgId
{
    if (imgId > 0) {
        _imgId = imgId;
        NSString * imgUrl = [[ImageManager sharedInstance] imageFileForImageId:imgId];
        [_imgUser setLocalImageName:imgUrl];
        return;
    }
    [[iToast makeText:@"无效的系统图像!"] show];
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)didFinishUserInfo:(UserQueryByIdResponseMessage*)userMsg{

    if(userMsg != nil && [userMsg resultCode] == 0){
    
        UserMessage* user = [userMsg user];
        
        if([user portrait]){
            NSString* url = [[user portrait] imageUrl];
            [_imgUser loadAsyncImage:url];
        }
        if([user phone]){
            _textPhone.text = [user phone];
        }
        if([user userName]){
            _textAccount.text = [user userName];
        }
        if([user nickName]){
            _textName.text = [user nickName];
        }
        if([user signName]){
            _textSaid.text = [user signName];
        }
        
        return;
    }
    [[[iToast makeText:@"服务器出错,无法获取个人信息"] setDuration:2000] show];
}
- (void)failUser{

    [[[iToast makeText:@"服务器出错,无法获取个人信息"] setDuration:2000] show];
}


- (void)didFinishModify:(UserProModifyResponseMessage*)result{

    if(result != nil && [result resultCode] == 0){
        [[[iToast makeText:@"个人信息已修改成功"] setDuration:2000] show];
        if (self.navigationController.viewControllers.count > 1) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [self.navigationController dismissModalViewControllerAnimated:YES];
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_USER_INFO_REFRESH object:nil];
        return;
    }
    
    [[[iToast makeText:@"服务器出错,修改个人信息失败"] setDuration:2000] show];
}
- (void)failModify{

    [[[iToast makeText:@"服务器出错,修改个人信息失败"] setDuration:2000] show];
}


- (void)didUploadImage:(CustomedImageResponseMessage*)userMsg{

    if(userMsg != nil && [userMsg resultCode] == 0){
    
        long long imgId = [userMsg imageId];
        if(imgId > 0){
            [self sendRequst:imgId];
        }
        return;
    }
    [[[iToast makeText:@"服务器出错,上传头相失败"] setDuration:2000] show];
}



- (IBAction)actionBack:(id)sender {
    
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self.navigationController dismissModalViewControllerAnimated:YES];
    }
}

#pragma mark - UITextFieldDelegate

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [_scroll setContentOffset:CGPointMake(0, 200) animated:YES];
}


- (void)dealloc {
    [_textPhone release];
    [_textAccount release];
    [_textName release];
    [_textSaid release];
    [_textPwd1 release];
    [_textPwd2 release];
    [_imgUser release];
    [_scroll release];
    [_btnSubmit release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setTextPhone:nil];
    [self setTextAccount:nil];
    [self setTextName:nil];
    [self setTextSaid:nil];
    [self setTextPwd1:nil];
    [self setTextPwd2:nil];
    [self setImgUser:nil];
    [self setScroll:nil];
    [self setBtnSubmit:nil];
    [super viewDidUnload];
}
- (IBAction)focusPwd1:(id)sender {
    [_textPwd1 becomeFirstResponder];
}

- (IBAction)focusPwd2:(id)sender {
    [_textPwd2 becomeFirstResponder];
}
- (IBAction)hideKeyboard:(id)sender {
    [_textPwd2 resignFirstResponder];
    [_scroll setContentOffset:CGPointZero animated:YES];
}
- (IBAction)actionSubmit:(id)sender {
    
    NSString* pwd1 = _textPwd1.text;
    NSString* pwd2 = _textPwd2.text;
    
    if(![pwd1 isEqualToString:pwd2]){
        [[[iToast makeText:@"两次密码输入不一致!"] setDuration:2000] show];
        return;
    }
    if(_imgId > 0){
       [self sendRequst:_imgId];
    } else {
    
        [self sendRequst:-1];
    }
    
}


- (void)sendRequst:(long long)imgId{

    NSString* pwd1 = _textPwd1.text;
    NSString* pwd2 = _textPwd2.text;
    
    if(![pwd1 isEqualToString:pwd2]){
        [[[iToast makeText:@"两次密码输入不一致!"] setDuration:2000] show];
        return;
    }
    
    
    NSString* nick = _textName.text;
    if(nick == nil || nick.length <= 0){
        nick = nil;
    }
    
    NSString* said = _textSaid.text;
    if(said == nil || said.length <= 0){
        said = nil;
    }
    
    if(pwd1 == nil || pwd1.length <= 0){
        pwd1 = nil;
    }
    
    [_parser reqModiInfo:[GConfig getUserId] imageId:imgId nickName:nick signName:said password:pwd1];
}


@end
